﻿using PLCS.Domain.Shared.Enums;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace PLCS.App.Shared.Views.UserControls.Chlidren;

/// <summary>
/// Track.xaml 的交互逻辑
/// </summary>
public partial class Track : UserControl
{
    #region Properties

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(Track), new UIPropertyMetadata(null));

    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation,styling,binding,etc...
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), typeof(Track), new UIPropertyMetadata(null));

    /// <summary>
    /// 强制输入事件和属性来自FrameworkElement
    /// </summary>
    public IInputElement CommandTarget
    {
        get { return (IInputElement)GetValue(CommandTargetProperty); }
        set { SetValue(CommandTargetProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CommandTarget.  This enables animation,etc...
    public static readonly DependencyProperty CommandTargetProperty =
        DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(Track), new UIPropertyMetadata(null));

    //反射板
    public Rectangle plate = new Rectangle();

    /// <summary>
    /// 名称
    /// </summary>
    public string SName
    {
        get { return (string)GetValue(SNameProperty); }
        set { SetValue(SNameProperty, value); }
    }

    public static readonly DependencyProperty SNameProperty =
        DependencyProperty.Register("SName", typeof(string), typeof(Track), new PropertyMetadata("T101"));

    /// <summary>
    /// 反射板相对轨道位置
    ///  0 左 ； 1右； 2上； 3下
    /// </summary>
    public string PlatePos
    {
        get { return (string)GetValue(PlatePosProperty); }
        set { SetValue(PlatePosProperty, value); }
    }

    public static readonly DependencyProperty PlatePosProperty =
        DependencyProperty.Register("PlatePos", typeof(string), typeof(Track), new PropertyMetadata("上"));

    #endregion Properties

    #region Constructor

    public Track()
    {
        InitializeComponent();
        LayoutCanvas.ToolTip = $"轨道:{SName}";
        Cursor = Cursors.Hand;
        SizeChanged += Track_SizeChanged;
    }

    #endregion Constructor

    #region Event

    //size changed event
    private void Track_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        try
        {
            //清空重新加载
            this.LayoutCanvas.Children.Clear();
            //鼠标悬停提示
            LayoutCanvas.ToolTip = $"轨道:{SName}";
            //实际宽小于高 - 认定为竖放标志
            bool verticalflag = LayoutCanvas.ActualWidth < LayoutCanvas.ActualHeight ? true : false;
            // 反射板
            plate = new Rectangle();
            plate.Width = verticalflag ? LayoutCanvas.ActualWidth : 3;
            plate.Height = verticalflag ? 3 : LayoutCanvas.ActualHeight;
            plate.Fill = Brushes.DimGray;
            plate.ToolTip = "激光反射板";
            if (verticalflag && PlatePos == ReflectingPlatePos.下.ToString())
            {
                plate.Margin = new Thickness()
                {
                    Top = LayoutCanvas.ActualHeight - 3,
                };
            }
            if (!verticalflag && PlatePos == ReflectingPlatePos.右.ToString())
            {
                plate.Margin = new Thickness()
                {
                    Left = LayoutCanvas.ActualWidth - 3,
                };
            }
            LayoutCanvas.Children.Add(plate);

            //轨道（堆垛机中间的轨道）
            var middle = new Rectangle();
            middle.Width = verticalflag ? 4 : LayoutCanvas.ActualWidth;
            middle.Height = verticalflag ? LayoutCanvas.ActualHeight : 4;
            middle.Fill = Brushes.DimGray;
            // middle.RadiusY= middle.RadiusX = 3;
            middle.Stroke = Brushes.DimGray;
            var tn = new Thickness();
            if (verticalflag)
            {
                tn.Left = LayoutCanvas.ActualWidth / 2 - 2;
            }
            else
            {
                tn.Top = LayoutCanvas.ActualHeight / 2 - 2;
            }
            middle.Margin = tn;
            LayoutCanvas.Children.Add(middle);
            //两侧线
            if (verticalflag)
            {//竖放
                var left = new Line();
                left.Y1 = left.X1 = left.X2 = 0;
                left.Y2 = ActualHeight;
                left.StrokeThickness = 1;
                left.StrokeDashArray = new DoubleCollection(new[] { 2.1, 2 });
                left.Stroke = Brushes.DimGray;
                LayoutCanvas.Children.Add(left);
                var right = new Line();
                right.X1 = right.X2 = ActualWidth;
                right.Y2 = ActualHeight;
                right.Y1 = 0;
                right.StrokeThickness = 1;
                right.StrokeDashArray = new DoubleCollection(new[] { 4.1, 2 });
                right.Stroke = Brushes.DimGray;
                LayoutCanvas.Children.Add(right);
                var line = new Line();
                line.X1 = 0;
                line.X2 = ActualWidth;
                line.Y2 = line.Y1 = PlatePos == ReflectingPlatePos.下.ToString() ? 0 : ActualHeight - 1;
                line.StrokeThickness = 1;
                line.StrokeDashArray = new DoubleCollection(new[] { 4.1, 2 });
                line.Stroke = Brushes.DimGray;
                LayoutCanvas.Children.Add(line);
            }
            else
            {
                //横放
                var top = new Line();
                top.X1 = 0;
                top.Y2 = top.Y1 = 0;
                top.X2 = ActualWidth;
                top.StrokeThickness = 1;
                top.StrokeDashArray = new DoubleCollection(new[] { 4.1, 2 });
                top.Stroke = Brushes.DimGray;
                LayoutCanvas.Children.Add(top);
                var bottom = new Line();
                bottom.X1 = 0;
                bottom.Y2 = bottom.Y1 = ActualHeight;
                bottom.X2 = ActualWidth;
                bottom.StrokeThickness = 1;
                bottom.StrokeDashArray = new DoubleCollection(new[] { 4.1, 2 });
                bottom.Stroke = Brushes.DimGray;
                LayoutCanvas.Children.Add(bottom);
                var line = new Line();
                line.X2 = line.X1 = PlatePos == ReflectingPlatePos.左.ToString() ? ActualWidth - 1 : 0;
                line.Y1 = 0;
                line.Y2 = ActualHeight;
                //left.X2 = 1;
                //left.Y2 = ActualHeight - plate.Width;
                line.StrokeThickness = 1;
                line.StrokeDashArray = new DoubleCollection(new[] { 4.1, 2 });
                line.Stroke = Brushes.DimGray;
                LayoutCanvas.Children.Add(line);
            }
            ////轨道画线
            ////滚筒 间隔 3
            //if (verticalflag)
            //{//竖放输送线 滚筒摆放
            // //计算 可以放的滚筒个数
            //    int rattlerCount = (int)(this.LayoutCanvas.ActualHeight / 30);
            //    for (int i = 1; i < rattlerCount; i++)
            //    {
            //        this.LayoutCanvas.Children.Add(new Line()
            //        {
            //            X1 = 3,
            //            X2 = LayoutCanvas.ActualWidth - 3,
            //            Y1 = i * 30,
            //            Y2 = i * 30,
            //            Stroke = new SolidColorBrush(Color.FromRgb(255, 255, 255)),
            //            //Stroke = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Red")),
            //            StrokeThickness = 2,
            //        });
            //    }
            //}
            //else
            //{//横放输送线 滚筒摆放
            //    int rattlerCount = (int)(this.LayoutCanvas.ActualWidth / 30);
            //    for (int i = 1; i < rattlerCount; i++)
            //    {
            //        this.LayoutCanvas.Children.Add(new Line()
            //        {
            //            X1 = i * 30,
            //            X2 = i * 30,
            //            Y1 = 3,
            //            Y2 = LayoutCanvas.ActualHeight - 3,
            //            Stroke =new SolidColorBrush(Color.FromRgb(255,255,255)),
            //            StrokeThickness = 2,
            //        });
            //    }
            //}
        }
        catch (Exception ex)
        {
        }
    }

    /// <summary>
    /// 重载鼠标左击时触发事件
    /// </summary>
    /// <param name="e"></param>
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);
        //_eventAggregator.GetEvent<StationEvent>().Publish(SName);
        var command = Command;
        var parameter = CommandParameter;
        var target = CommandTarget;

        var routedCmd = command as RoutedCommand;
        if (routedCmd != null && routedCmd.CanExecute(parameter, target))
        {
            routedCmd.Execute(parameter, target);
        }
        else if (command != null && command.CanExecute(parameter))
        {
            command.Execute(parameter);
        }
    }

    #endregion Event
}